package com.example.opengldemo.vertex_magical.shader;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.opengl.GLES30;
import android.opengl.GLUtils;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

public class WaterRenderer extends LazyRenderer {

    private int[] viewPort = new int[4];
    private int[] markPort = new int[4];


    private  BaseRenderer mark ;
    Resources resources ;
    public WaterRenderer(Resources resources) {
        super();
        this.resources = resources ;
        //mark = new WaterRealShader(resources , "vertexmagical/shake_vertex.sh" , "vertexmagical/shake_frag.sh");
        //mark = new TwistRenderer(resources , "vertexmagical/twist_vertex.sh" , "vertexmagical/twist_frag.sh");
    }

    private int markTextureId = -1;
    float ratio  ;
    @Override
    protected void onSizeChanged(int width, int height) {
        ratio = (float) width / height;
        //mark.onSizeChanged(width , height);

    }

    public WaterRenderer setMarkPosition(final int x, final int y, final int width, final int height) {
        markPort[0] = x;
        markPort[1] = y;
        markPort[2] = width;
        markPort[3] = height;
        runOnGLThread(new Runnable() {
            @Override
            public void run() {
                mark.sizeChanged(width, height);
            }
        });
        return this;
    }

    public WaterRenderer setMark(final Bitmap bmp) {
        runOnGLThread(new Runnable() {
            @Override
            public void run() {
                if (bmp != null) {
                    if (markTextureId == -1) {
                        markTextureId = TextureUtil.createTextureID(false);
                    } else {
                        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, markTextureId);
                    }
                    GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, bmp, 0);
                    bmp.recycle();
                } else {
                    if (markTextureId != -1) {
                        GLES30.glDeleteTextures(1, new int[]{markTextureId}, 0);
                    }
                }
            }
        });
        return this;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mark.create();
    }

    @Override
    protected void onSetExpandData() {
        super.onSetExpandData();
        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 5);
        MatrixState.setCamera(0,0, 4, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        MatrixState.pushMatrix() ;
        shaderHandle.bindMVPMaxtrixHandle() ;
        MatrixState.popMatrix() ;
    }

    @Override
    protected void onDraw() {
        super.onDraw();
        if (markTextureId != -1) {
            GLES30.glGetIntegerv(GLES30.GL_VIEWPORT, viewPort, 0);//获取到当前的view窗口大小
            //设置一个新的大小窗口
            //GLES30.glViewport(markPort[0],mHeight-markPort[3]-markPort[1],markPort[2],markPort[3]);
            GLES30.glEnable(GLES30.GL_BLEND);
            GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
            GLES30.glBlendEquation(GLES30.GL_FUNC_ADD);
            mark.draw(markTextureId);
            GLES30.glDisable(GLES30.GL_BLEND);

            GLES30.glViewport(viewPort[0], viewPort[1], viewPort[2], viewPort[3]);
        }
        //todo reset blend and view port
    }
}
